package com.vincent.seeyou.ui.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.vincent.seeyou.R;
import com.vincent.seeyou.SYApplication;
import com.vincent.seeyou.SYMessageReceiver;
import com.vincent.seeyou.ui.MainActivity;
import com.vincent.seeyou.ui.NewFriendActivity;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.im.BmobChatManager;
import cn.bmob.im.BmobNotifyManager;
import cn.bmob.im.bean.BmobInvitation;
import cn.bmob.im.bean.BmobMsg;
import cn.bmob.im.inteface.EventListener;

/**
 * Created by vincent on 14-8-28.
 */
public class SessionFragment extends BaseFragment implements EventListener {


    private List<Fragment> fragments ;
    private String[] titles;
    private int currentTabIndex;;

    private RecentFragment recentFragment;
    private ContactsFragment contactFragment;

    public static SessionFragment newInstance(int sectionNumber) {
        SessionFragment fragment = new SessionFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    public SessionFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        recentFragment = new RecentFragment();
        contactFragment = new ContactsFragment();
        fragments = new ArrayList<Fragment>();
        fragments.add(recentFragment);
        fragments.add(contactFragment);

        titles = new String[]{getString(R.string.sesstion_recent),
                getString(R.string.sesstion_contacts)};
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        ViewPager viewPager  = (ViewPager)rootView.findViewById(R.id.viewpager);

        viewPager.setAdapter(new SessionAdapter(getChildFragmentManager(),fragments,titles));
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i2) {

            }

            @Override
            public void onPageSelected(int i) {
                currentTabIndex = i;
                if(contactFragment != null){
                    contactFragment.refresh();
                }
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

        return rootView;
    }



    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        ((MainActivity) activity).onSectionAttached(
                getArguments().getInt(ARG_SECTION_NUMBER));
    }

    @Override
    public void onResume() {
        super.onResume();
        SYMessageReceiver.ehList.add(this);// 监听推送的消息
        //清空
        SYMessageReceiver.mNewNum=0;
    }

    @Override
    public void onPause() {
        super.onPause();
        SYMessageReceiver.ehList.remove(this);// 取消监听推送的消息
    }

    @Override
    public void onMessage(BmobMsg bmobMsg) {

        // 声音提示
        boolean isAllow = SYApplication.getInstance().getSpUtil().isAllowVoice();
        if(isAllow){
            SYApplication.getInstance().getMediaPlayer().start();
        }

        //保存接收到的消息-并发送已读回执给对方
        BmobChatManager.getInstance(getActivity()).saveReceiveMessage(true,bmobMsg);

        if(currentTabIndex==0){
            //当前页面如果为会话页面，刷新此页面
            if(recentFragment != null){
                recentFragment.refresh();
            }
        }
    }

    @Override
    public void onReaded(String conversionId, String msgTime) {

    }

    @Override
    public void onNetChange(boolean isNetConnected) {
        if(isNetConnected){
            ShowToast(R.string.tip_network);
        }
    }

    @Override
    public void onAddUser(BmobInvitation bmobInvitation) {
        // 声音提示
        boolean isAllow = SYApplication.getInstance().getSpUtil().isAllowVoice();
        if(isAllow){
            SYApplication.getInstance().getMediaPlayer().start();
        }

        if(currentTabIndex==1){
            if(contactFragment != null){
                contactFragment.refresh();
            }
        }else{
            //同时提醒通知
            String tickerText = bmobInvitation.getFromname()+"请求添加好友";
            boolean isAllowVibrate = SYApplication.getInstance().getSpUtil().isAllowVibrate();
            BmobNotifyManager.getInstance(getActivity()).showNotify(isAllow,isAllowVibrate,R.drawable.ic_launcher, tickerText, bmobInvitation.getFromname(), tickerText.toString(),NewFriendActivity.class);
        }
    }

    @Override
    public void onOffline() {

    }



















    private class SessionAdapter extends FragmentStatePagerAdapter{

        private List<Fragment> fragments;
        private String[] titles;

        private SessionAdapter(FragmentManager fm, List<Fragment> fragments, String[] titles) {
            super(fm);
            this.fragments = fragments;
            this.titles = titles;

        }

        @Override
        public Fragment getItem(int i) {
            return fragments.get(i);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }
    }
}
